<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: DeletePage.php 360 2010-02-03 08:07:12Z steen.rabol@gmail.com $
 *
 */

class DeletePage extends PortalSystemPage
{
	private $_pagename		= '';
	private $_controlname	= '';
	private $_portletname	= '';
	private $_recid			= 0;

	public function OnInit($param)
	{
		parent::onInit($param);
		if($this->User->IsGuest)
		{
			$this->reportError(1,"You do not have permission to view this page");
		}

		$am	= $this->Application->getModule('auth');
		if(!$am->CanEdit($this->Request['pagename']))
		{
			$this->reportError(1,"You do not have permission to view this page");
		}

		if($this->Request['pagename'] == 'Home')
		{
			$this->reportError(1,"The 'Home' page cannot be deleted");
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		$this->Title				= $this->Application->Parameters['SiteTitle'] . " - " . Prado::localize("Delete page");
		$this->Deletemsg->Text			= Prado::localize("Are you sure that you would like to delete page '{0}' ?", array($this->Request['pagename']));
		$this->_pagename				= $this->Request['pagename'];
		$this->CancelLink->NavigateUrl	= $this->Service->constructUrl($this->_pagename,array("PageMode" => $this->Page->EditMode));
	}

	public function deleteButtonClicked($sender,$param)
	{
		if($this->Page->IsValid)
		{
			$pagerow = $this->DataAccess->createCommand("select * from tblpages where name='" . $this->_pagename . "'")->queryRow();
			$sql = "delete from tblpages where name='" . $this->_pagename . "'";
			try
			{
				$this->DataAccess->createCommand($sql)->execute();
			}
			catch(Exception $e)
			{
				$this->reportError(1,$e->getMessage());
			}
			// delete php file
			if(file_exists($this->Service->BasePath . "/" . $pagerow['name'] . ".php"))
				@unlink($this->Service->BasePath . "/" . $pagerow['name'] . ".php");

			// delete page file
			if(file_exists($this->Service->BasePath . "/" . $pagerow['name'] . ".page"))
				@unlink($this->Service->BasePath . "/" . $pagerow['name'] . ".page");

			// delete references to this page
			try
			{
				$this->DataAccess->createCommand("delete from tblpagecontent where pagename = '{$pagerow['name']}'")->execute();
				$this->DataAccess->createCommand("delete from tbllxlistmenuportlet where pagename = '{$pagerow['name']}'")->execute();
				//$this->DataAccess->createCommand("delete from tbljscookmenuitem where pagename = '{$pagerow['name']}'")->execute();
				$this->DataAccess->createCommand("delete from tblsitemenuitem where page = '{$pagerow['name']}'")->execute();
			}
			catch(Exception $e)
			{
				$this->reportError(1,$e->getMessage());
			}
		}
		$this->gotoPage($this->Service->DefaultPage);
	}

	public function cancelButtonClicked($sender,$param)
	{
		$this->gotoPage($this->pid->Value,array("PageMode" => $this->Page->EditMode));
	}

	public function getPageName()
	{
		return $this->_pagename;
	}
}
?>